package com.cxs.musicplaywidget.commonevent;



import com.cxs.musicplaywidget.view.AbilityControllerAdapter;
import com.cxs.musicplaywidget.view.IControllerAdapter;
import com.cxs.musicplaywidget.view.WidgetControllerAdapter;
import com.cxs.musicplaywidget.data.EventParam;
import com.cxs.musicplaywidget.data.MDevice;
import com.cxs.musicplaywidget.data.PlayerControl;
import com.cxs.musicplaywidget.util.LogUtils;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.rpc.MessageParcel;
import ohos.utils.zson.ZSONObject;

import static com.cxs.musicplaywidget.constant.Const.ABILITY_TYPE;
import static com.cxs.musicplaywidget.data.EventParam.EventAction.*;
import static com.cxs.musicplaywidget.data.EventParam.EventAction;
import static com.cxs.musicplaywidget.data.EventParam.PlayerState.*;

/**
 * @ClassName Subscriber
 * @Author cxs
 * @Version 1.0
 * @Description 订阅者，在这里更新界面
 * @Date 2022/03/10
 */
public class Subscriber extends CommonEventSubscriber {
    private static final String tag = Subscriber.class.getSimpleName();
    private int abilityWidgetType;  // 0: ability; 1: widget
    private final Context context;
    IControllerAdapter controllerAdapter;
    private static long thumbIndex;

    public Subscriber(Context context, int type, CommonEventSubscribeInfo subscribeInfo) {
        super(subscribeInfo);
        this.context = context;
        this.abilityWidgetType = type;
        if (abilityWidgetType == ABILITY_TYPE) {
            controllerAdapter = new AbilityControllerAdapter(context);
        } else {
            controllerAdapter = new WidgetControllerAdapter(context);
        }
    }

    /**
     * 在这里接收发布的公共事件
     * @param commonEventData
     */
    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        String data = commonEventData.getData();
        LogUtils.debug(tag, "onReceiveEvent.data = " + data);
        EventParam eventParam = ZSONObject.stringToClass(data, EventParam.class);
        start(eventParam);
    }

    private void start(EventParam eventParam) {

        if (eventParam == null) {
            return;
        }

        LogUtils.debug(tag, "start.eventParam = " + eventParam);
        InnerEvent innerEvent = InnerEvent.get(eventParam.getEventAction().getCode(), eventParam);
        eventHandler.sendEvent(innerEvent);

    }

    EventHandler eventHandler = new EventHandler(EventRunner.create()) {
        final long timeout = 10000;  // 设置超时时间为10秒
        final long delayTime = 200;
        final long count = timeout / delayTime;
        @Override
        protected void processEvent(InnerEvent event) {
            EventAction action = getByValue(event.eventId);

            EventParam eventParam = (EventParam) event.object;
            switch (action) {
                case LOAD_DEVICE_LIST: {
                    controllerAdapter.updateDeviceList(true);
                    break;
                }
                case CONNECT: {
                    LogUtils.debug(tag, "start CONNECT");
                    eventHandler.sendEvent(InnerEvent.get(CONNECTING.getCode(), eventParam));

                    break;
                }
                case CONNECTING: {
                    LogUtils.debug(tag, "eventHandler.CONNECTING.eventparam = " + eventParam);

//                    controllerAdapter.updateDeviceList(false);
                    controllerAdapter.updatePlayerControl(NONE);
                    break;
                }
                case CONNECTED: {
                    LogUtils.debug(tag, "eventHandler.CONNECTED.eventParam =" + eventParam);
                    controllerAdapter.updateDeviceList(false);
                    controllerAdapter.updatePlayerControl(PREPARED);
//                    controllerAdapter.updatePlaylist(eventParam.getPlayList());
                    break;
                }
                case PLAY:
                case PREVIOUS:
                case NEXT:
                    LogUtils.debug(tag, "eventHandler.action=" + action.name());
                    controllerAdapter.updatePlayerControl(PLAYING);
                    break;
                case PAUSE: {
                    controllerAdapter.updatePlayerControl(PAUSED);
                    break;
                }
                case GET_PLAYER_STATE: {
                    controllerAdapter.updatePlayerControl(eventParam.getPlayerState());
                    break;
                }
                case GET_PLAY_LIST:
                case RELOAD_PLAY_LIST: {
                    controllerAdapter.updatePlaylist(eventParam);
                    break;
                }
                case GET_MUSIC_INFO: {
                    break;
                }
                case GET_VOLUME: {
                    controllerAdapter.updateVolume(eventParam.getMaxVolume(), eventParam.getVolume());
                    break;
                }
            }
        }
    };


}
